/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.reservacion.vista;

import basedatos.OperacionesBD;
import basedatos.ReservacionBD;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import modulos.usuario.modelo.Usuario;
import modulos.consulta.vista.IFrmHCGeneral;
import modulos.reservacion.modelo.Hora;
import modulos.reservacion.modelo.Reservacion;
import vista.frmPrincipal;
import vista.modelo.OperacionesVarias;

/**
 *
 * @author Edulima
 */
public class iFrmSesionMedica extends javax.swing.JInternalFrame {

    private Usuario usuario = new Usuario();
    private Reservacion reservacion = new Reservacion();
    private ArrayList<Reservacion> listaReservaciones = new ArrayList<Reservacion>();
    private DefaultTableModel modeloTablaReservaciones = new DefaultTableModel();
    private ArrayList<Usuario> usuarios = new ArrayList<Usuario>();

    /**
     * Creates new form iFrmSesionMedica
     */
    public iFrmSesionMedica() {
        modeloTablaReservaciones.addColumn("Nro.");
        modeloTablaReservaciones.addColumn("N° HC");
        modeloTablaReservaciones.addColumn("FECHA");
        modeloTablaReservaciones.addColumn("HORA");
        modeloTablaReservaciones.addColumn("SERVICIO");
        modeloTablaReservaciones.addColumn("ESTADO");
        initComponents();
        tblReservaciones.getColumnModel().getColumn(0).setPreferredWidth(40);
        tblReservaciones.getColumnModel().getColumn(1).setPreferredWidth(60);
        tblReservaciones.getColumnModel().getColumn(2).setPreferredWidth(80);
        tblReservaciones.getColumnModel().getColumn(3).setPreferredWidth(60);
        tblReservaciones.getColumnModel().getColumn(4).setPreferredWidth(200);
        tblReservaciones.getColumnModel().getColumn(5).setPreferredWidth(100);
        tblReservaciones.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

        OperacionesVarias.cerrarVentanaEscape(this);
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        txtEspecialista.setText(usuario + "");
        listarReservaciones();
    }

    private void setReservacion(Reservacion reservacion) {
        txtNHC.setText(reservacion.getPaciente().getNumeroHC());
        txtPaciente.setText(reservacion.getPaciente() + "");
        if (reservacion.getEstado().equals(iFrmReservaCita.ESTADO_ATENDIDO)) {
            rbtnSi.setSelected(true);
        } else {
            rbtnNo.setSelected(true);
        }
    }

    private void listarReservaciones()//muestra las reservaciones del medico seleccionado
    {
        int c = modeloTablaReservaciones.getRowCount();
        for (int i = 0; i < c; i++) {
            modeloTablaReservaciones.removeRow(0);
        }
        DateFormat dateFormatDB = new SimpleDateFormat("yyyy-MM-dd");
        listaReservaciones = (ArrayList<Reservacion>) ReservacionBD.listarPorFecha(usuario, dateFormatDB.format(new Date()), iFrmReservaCita.ESTADO_RESERVADO);
        int cont = 0;
        DateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        for (Reservacion r : listaReservaciones) {
            cont++;
            Object[] fila = {
                cont,
                r.getPaciente().getNumeroHC(),
                formato.format(r.getFecha()),
                new Hora(r.getHora()),
                r.getServicio(),
                r.getEstado()
            };
            modeloTablaReservaciones.addRow(fila);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        btnCancelar = new javax.swing.JButton();
        txtEspecialista = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        lblNhc = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtPaciente = new javax.swing.JTextField();
        txtNHC = new javax.swing.JTextField();
        btnFinalizar = new javax.swing.JButton();
        btnConsulta = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        rbtnNo = new javax.swing.JRadioButton();
        rbtnSi = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        scrolpReserva = new javax.swing.JScrollPane();
        tblReservaciones = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };

        setClosable(true);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/DutyRoster.png"))); // NOI18N

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCancelar.setText("Cerrar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblNhc.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        lblNhc.setText("N° HC");

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel1.setText("Paciente: ");

        txtPaciente.setEditable(false);

        txtNHC.setEditable(false);

        btnFinalizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/accept.png"))); // NOI18N
        btnFinalizar.setText("Finalizar");
        btnFinalizar.setEnabled(false);
        btnFinalizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFinalizarActionPerformed(evt);
            }
        });

        btnConsulta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/PatientFile.png"))); // NOI18N
        btnConsulta.setText("CONSULTA");
        btnConsulta.setEnabled(false);
        btnConsulta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultaActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Estado: Atendido", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Andale Mono", 1, 12), java.awt.Color.black)); // NOI18N

        buttonGroup1.add(rbtnNo);
        rbtnNo.setText("No");
        rbtnNo.setContentAreaFilled(false);
        rbtnNo.setEnabled(false);
        rbtnNo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnNoActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbtnSi);
        rbtnSi.setText("SI");
        rbtnSi.setContentAreaFilled(false);
        rbtnSi.setEnabled(false);
        rbtnSi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbtnSiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(rbtnSi)
                .addGap(14, 14, 14)
                .addComponent(rbtnNo)
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbtnSi)
                    .addComponent(rbtnNo)))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(lblNhc))
                .addGap(6, 6, 6)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtNHC, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(300, 300, 300))
                    .addComponent(txtPaciente, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 360, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnConsulta)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnFinalizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(txtNHC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(15, 15, 15)
                                .addComponent(lblNhc)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(btnConsulta, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(btnFinalizar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 3, 18)); // NOI18N
        jLabel2.setText("ESPECIALISTA:");

        tblReservaciones.setFont(new java.awt.Font("Ubuntu", 0, 14)); // NOI18N
        tblReservaciones.setModel(modeloTablaReservaciones);
        tblReservaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                tblReservacionesMouseReleased(evt);
            }
        });
        tblReservaciones.addHierarchyListener(new java.awt.event.HierarchyListener() {
            public void hierarchyChanged(java.awt.event.HierarchyEvent evt) {
                tblReservacionesHierarchyChanged(evt);
            }
        });
        tblReservaciones.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tblReservacionesKeyReleased(evt);
            }
        });
        scrolpReserva.setViewportView(tblReservaciones);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(729, 729, Short.MAX_VALUE)
                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtEspecialista, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 148, Short.MAX_VALUE))
                    .addComponent(scrolpReserva))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2)
                    .addComponent(txtEspecialista, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(scrolpReserva, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                .addComponent(btnCancelar)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnConsultaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultaActionPerformed
    int index = tblReservaciones.getSelectedRow();
    if (index != -1) {
        IFrmHCGeneral hCGeneral = new IFrmHCGeneral(reservacion.getPaciente());
        frmPrincipal.dskpPrincipal.add(hCGeneral);
        hCGeneral.toFront();
        hCGeneral.setVisible(true);
    } else {
        JOptionPane.showMessageDialog(this, "Seleccione antes la Reservacion ...", "Informacion", JOptionPane.WARNING_MESSAGE);
    }
}//GEN-LAST:event_btnConsultaActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void tblReservacionesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblReservacionesKeyReleased
    }//GEN-LAST:event_tblReservacionesKeyReleased

    private void tblReservacionesHierarchyChanged(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_tblReservacionesHierarchyChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_tblReservacionesHierarchyChanged

    private void tblReservacionesMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblReservacionesMouseReleased
        int index = tblReservaciones.getSelectedRow();
        if (index != -1) {
            reservacion = listaReservaciones.get(index);
            setReservacion(reservacion);
            btnConsulta.setEnabled(true);
            btnFinalizar.setEnabled(true);
            rbtnNo.setEnabled(true);
            rbtnSi.setEnabled(true);

        }
    }//GEN-LAST:event_tblReservacionesMouseReleased

    private void btnFinalizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFinalizarActionPerformed
        if (reservacion.getEstado().equals(iFrmReservaCita.ESTADO_RESERVADO)) {
            reservacion.setEstado(iFrmReservaCita.ESTADO_NO_ATENDIDO);
        }
        if (JOptionPane.showConfirmDialog(this, "¿Realmente desea finalizar la sesión medica?\n"
                + "El paciente " + reservacion.getPaciente() + " se encuantra en estado \"" + reservacion.getEstado() + "\"", "Finalizar Sesion Medica", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0) {
            OperacionesBD.modificar(reservacion);
            listarReservaciones();
            btnConsulta.setEnabled(false);
            btnFinalizar.setEnabled(false);
            txtNHC.setText("");
            txtPaciente.setText("");
            rbtnNo.setEnabled(false);
            rbtnSi.setEnabled(false);
        }
    }//GEN-LAST:event_btnFinalizarActionPerformed

    private void rbtnSiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnSiActionPerformed
        reservacion.setEstado(iFrmReservaCita.ESTADO_ATENDIDO);
    }//GEN-LAST:event_rbtnSiActionPerformed

    private void rbtnNoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbtnNoActionPerformed
        reservacion.setEstado(iFrmReservaCita.ESTADO_NO_ATENDIDO);
    }//GEN-LAST:event_rbtnNoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConsulta;
    private javax.swing.JButton btnFinalizar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel lblNhc;
    private javax.swing.JRadioButton rbtnNo;
    private javax.swing.JRadioButton rbtnSi;
    private javax.swing.JScrollPane scrolpReserva;
    private javax.swing.JTable tblReservaciones;
    private javax.swing.JLabel txtEspecialista;
    private javax.swing.JTextField txtNHC;
    private javax.swing.JTextField txtPaciente;
    // End of variables declaration//GEN-END:variables
}
